/***********************************************************************************************************************
    @file    i2c_master.c
    @author  FAE Team
    @date    2-2025
    @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  **********************************************************************************************************************
    @attention

    <h2><center>&copy; Copyright(c) <2025> <MySentech></center></h2>

      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
    following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
       the following disclaimer in the documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
       promote products derived from this software without specific prior written permission.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *********************************************************************************************************************/
#include "i2c_master.h"

/**-----------------------------------------------------------------------
  * @brief  Initialize the IIC GPIO
  * @param  无
  * @retval None
-------------------------------------------------------------------------*/
void GPIOI2C_Bus_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIOI2C_SCL_GPIO_CLK_ENABLE();
	GPIOI2C_SDA_GPIO_CLK_ENABLE();

	GPIO_InitStructure.GPIO_Pin = GPIOI2C_SCL_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

	GPIO_Init(GPIOI2C_SCL_GPIO_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIOI2C_SDA_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
	GPIO_Init(GPIOI2C_SDA_GPIO_PORT, &GPIO_InitStructure);

}


/**-----------------------------------------------------------------------
  * @brief  Send I2C start condition
  * @param  无
  * @retval None
-------------------------------------------------------------------------*/
void I2C_Start(void)
{
	i2c_set_sda();
	i2c_set_scl();
	
	MY_DELAY_US(1);
	/*Bus free time(between Stop to Start time) tBUF>1.3us met by implement in stop() function*/
	i2c_clear_sda();
	MY_DELAY_US(1); 
	i2c_clear_scl();
	
}

/**-----------------------------------------------------------------------
  * @brief  Send I2C repeated start condition
  * @param  无
  * @retval None
-------------------------------------------------------------------------*/
void I2C_reStart(void)
{
  /*ReStart condition */
	i2c_set_sda();
	MY_DELAY_US(1);  
	i2c_set_scl();
	MY_DELAY_US(1);  
	i2c_clear_sda();
	MY_DELAY_US(1); 
	i2c_clear_scl();
	MY_DELAY_US(1); 

}

/**-----------------------------------------------------------------------
  * @brief  Send I2C stop condition
  * @param  无
  * @retval None
-------------------------------------------------------------------------*/
void I2C_Stop(void)
{
  /*Stop condition */
	i2c_clear_sda();
	MY_DELAY_US(1);  /*SU;STA*/
	i2c_set_scl();
	MY_DELAY_US(1);   /*tSU;STO*/
	i2c_set_sda();	
	MY_DELAY_US(1);/*tBUF>1.3us for Bus free time*/

}

/**-----------------------------------------------------------------------
 * @brief  Transmit one byte via I2C
 * @param  b Byte to send
 * @retval Acknowledgment status (1: ACK received, 0: NACK received)
-------------------------------------------------------------------------*/
unsigned char I2C_master_write(unsigned char b)
{
  	unsigned char mask = 0x80, ack=0;
	do
  	{	
		MY_DELAY_US(1);  /*tHD;DAT*/				
		sda_wr_control(b);	
		MY_DELAY_US(1);  /*tSU_DAT*/	
		i2c_set_scl();
		MY_DELAY_US(1);  /*nHigh*/
		i2c_clear_scl();			
  	}while ((mask>>=1) != 0);

	/* ACK slot checking */
	MY_DELAY_US(1); /*tHD;DAT*/		
	i2c_set_sda();/* Release SDA, waiting for tVD_ACK */
	MY_DELAY_US(1);  /*tSU_DAT*/
	i2c_set_scl();
	MY_DELAY_US(1); /*tHD;DAT*/		
	ack = (i2c_get_sda()? 0:1);	
	MY_DELAY_US(1);  /*nHigh*/
	i2c_clear_scl();
  	return (ack);
}

/**-----------------------------------------------------------------------
 * @brief  Receive one byte via I2C
 * @param  ack ACK/NACK to send after receiving
 * @retval Received byte
-------------------------------------------------------------------------*/
unsigned char I2C_master_read(unsigned char ack)
{ unsigned char mask = 0x80, b = 0;

	i2c_set_sda();									/*Release SDA*/

	do
	{
	 	MY_DELAY_US(1);	
	  	i2c_set_scl();
	  	MY_DELAY_US(1);
  		sda_rd_control(b);
	  	i2c_clear_scl();
	  	MY_DELAY_US(1);
	}while ((mask>>=1) != 0);

	/* ACK slot control */
	if(ack != 0) 
	{
		i2c_clear_sda();		/* SDA=0 for ACK*/
	}
	else
	{
		i2c_set_sda();		/* SDA=1 for NACK*/
	}	
	MY_DELAY_US(1);
	i2c_set_scl();
	MY_DELAY_US(1);
	i2c_clear_scl();
	MY_DELAY_US(1);

	return (b);
}


/**-----------------------------------------------------------------------
 * @brief  Write data to an I2C device
 * @param  DeviceAddr I2C device address
 * @param  RegAddr Register address
 * @param  Data Data to write
 * @retval Transmission status
-------------------------------------------------------------------------*/
unsigned char write_i2c(unsigned char DeviceAddr, MC1081_REG RegAddr, unsigned char Data)
{
	unsigned char ret=GPIOI2C_XFER_LASTACK;
	
	I2C_Start();

	if(I2C_master_write((DeviceAddr<<1) & I2CWRITE_MASK)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ADDRNACK;
	}

	if(I2C_master_write(RegAddr)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ABORTNACK;	
	}

	if(I2C_master_write(Data)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ABORTNACK;	
	}	

	I2C_Stop();
	return ret;	
}

/**-----------------------------------------------------------------------
 * @brief  Read data from an I2C device
 * @param  DeviceAddr I2C device address
 * @param  RegAddr Register address
 * @param  pData Pointer to store received data
 * @param  size Number of bytes to read
 * @retval Transmission status
-------------------------------------------------------------------------*/
unsigned char read_i2c(unsigned char DeviceAddr, MC1081_REG RegAddr,unsigned char *pData,unsigned char size)
{
	I2C_Start();
		
	if(I2C_master_write((DeviceAddr<<1) & I2CWRITE_MASK)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ADDRNACK;
	}
	
	if(I2C_master_write(RegAddr)==GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ABORTNACK;	
	}
	
	I2C_reStart();
	
	if(I2C_master_write((DeviceAddr<<1) | I2CREAD_MASK)== GPIOI2C_NACK)
	{
		I2C_Stop();
		return GPIOI2C_XFER_ABORTNACK;
	}	
	for(int i = 0; i< size-1; i++)
	{
		*pData++=I2C_master_read(GPIOI2C_ACK);
	}
	(*pData)=I2C_master_read(GPIOI2C_NACK);
	
	I2C_Stop();

	return GPIOI2C_XFER_LASTNACK;
}

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/********************************************** (C) Copyright MySentech **********************************************/


